package com.denny.devdemo.drawArc;

import com.denny.devdemo.R;
import com.denny.devdemo.drawArc.MarketProgressArc.OnProgressChangeListener;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MarketDownloadView extends RelativeLayout implements OnClickListener,OnProgressChangeListener{

	private Context mCotext;
	private TextView mBtnTxt;
	private MarketProgressArc mProgressArc;
	private OnClickListener mOptionClickListener;

	public MarketDownloadView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MarketDownloadView(Context context) {
		super(context);
		this.mCotext = context;
		initView();
		
	}

	public void setBackground(int resId){
		if(mProgressArc != null){
			mProgressArc.setBackgroundResource(resId);
		}
	}
	
	public void setBtnBackground(int resId){
		if(mBtnTxt != null){
			mBtnTxt.setBackgroundResource(resId);
		}
	}
	
	public void setStyle(int style){
		if(mProgressArc != null){
			mProgressArc.setStyle(style);
		}
	}
	
	public void setProgressColor(int progressColor) {
		if(mProgressArc != null){
			mProgressArc.setmProgressColor(progressColor);
		}
	}

	public void setDeltaProgressColor(int deltaProgressColor) {
		if(mProgressArc != null){
			mProgressArc.setmDeltaProgressColor(deltaProgressColor);
		}
	}
	
	public void setProgress(float progress) {
		if(mProgressArc != null){
			mProgressArc.setProgress(progress);
		}
	}

	public void setDeltaInitProgress(float deltaInitProgress) {
		if(mProgressArc != null){
			mProgressArc.setDeltaInitProgress(deltaInitProgress);
		}
	}
	
	public void setOptionState(final String txt){
		if(mBtnTxt!= null){
//			mBtnTxt.post(new Runnable() {
//				
//				@Override
//				public void run() {
					mBtnTxt.setText(txt);
//				}
//			});
		}
	}
	
	public void setOptionClick(OnClickListener clickListener){
		this.mOptionClickListener = clickListener;
	}
	
	private void initView() {
		mBtnTxt = new TextView(mCotext);
		mProgressArc = new MarketProgressArc(mCotext);
		setBackground(R.drawable.pregress_bg);
		setBtnBackground(R.drawable.btn_bg);
//		mBtnTxt.setBackgroundColor(Color.GREEN);
//		mProgressArc.setBackgroundColor(Color.BLUE);
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				200, 200);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		addView(mProgressArc, params);
		
		params = new RelativeLayout.LayoutParams(150, 150);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
//		params.setMargins(10, 10, 10, 10);
		mBtnTxt.setGravity(Gravity.CENTER);
		addView(mBtnTxt, params);
		mProgressArc.setProgressChangeListener(this);
		setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if(mOptionClickListener != null){
			mOptionClickListener.onClick(v);
		}
	}

	@Override
	public void onProgressChange(float smoothProgress) {
		setOptionState((int)smoothProgress+"%");
	}

}
